//Documentation complete
/**
 * @author Alex
 * @version 2009AP06
 */

package control;

import static problemDomain.ErrorBean.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import problemDomain.*;
import problemDomain.EmployeeAccountBean.Position;

/**
 * Control object superclass wich generic data checks.
 */
public abstract class Control {
	
	/* Constants */
	
	private static final DateFormat CALENDAR_FORMAT = new SimpleDateFormat("dd/mm/yyyy");
	private static final DateFormat DATE_FORMAT = new SimpleDateFormat("dd-MMM-yyyy");
	
	/* END Constants */
	
	
	/* Methods */
	
	/**
	 * Parses a <code>String</code> into a <code>long</code>.
	 * @param number the <code>String</code>
	 * @param code the <code>ErrorCode</code> to throw and log when the <code>String</code> is unparsable
	 * @param caller the calling class
	 * @param logValues if <code>true</code> - log the input values
	 * @return the <code>long</code> value
	 * @throws ErrorBean if the input datum is not parsable
	 */
	protected long checkLong (String number, ErrorCode code, Control caller, boolean logValues) throws ErrorBean {
		try {
			return Long.parseLong(number);
		} catch (NumberFormatException e) {
			if (logValues)
				throwError(
					code,
					number,
					caller);
			else
				throwError(
					code,
					caller);
			return 0;
		}
	}
	
	/**
	 * Parses a <code>String</code> into an <code>int</code>.
	 * @param number the <code>String</code>
	 * @param code the <code>ErrorCode</code> to throw and log when the <code>String</code> is unparsable
	 * @param caller the calling class
	 * @return the <code>int</code> value
	 * @throws ErrorBean if the input datum is not parsable
	 */
	protected int checkInt (String number, ErrorCode code, Control caller) throws ErrorBean {
		try {
			return Integer.parseInt(number);
		} catch (NumberFormatException e) {
			throwError(
				code,
				number,
				caller);
			return 0;
		}
	}
	
	/**
	 * Parses a <code>String</code> into a <code>double</code>.
	 * @param number the <code>String</code>
	 * @param code the <code>ErrorCode</code> to throw and log when the <code>String</code> is unparsable
	 * @param caller the calling class
	 * @return the <code>double</code> value
	 * @throws ErrorBean if the input datum is not parsable
	 */
	protected double checkDouble (String number, ErrorCode code, Control caller) throws ErrorBean {
		try {
			return Double.parseDouble(number);
		} catch (NumberFormatException e) {
			throwError(
				code,
				number,
				caller);
			return 0;
		}
	}
	
	/**
	 * Checks a <code>String</code> for emptyness (<code>equals("")</code>).
	 * @param str the <code>String</code>
	 * @param code the <code>ErrorCode</code> to throw and log when the <code>String</code> is empty
	 * @param caller the calling class
	 * @param logValues if <code>true</code> - log the input values
	 * @return the <code>String</code> value
	 * @throws ErrorBean if the input datum is not parsable
	 */
	protected String checkString (String str, ErrorCode code, Control caller, boolean logValues) throws ErrorBean {
		if ("".equals(str)) {
			if (logValues)
				throwError(
					code,
					str,
					caller);
			else
				throwError(
					code,
					caller);
		}
		return str;
	}
	
	/**
	 * Parses a <code>String</code> into a <code>Boolean</code>.
	 * @param bool the <code>String</code>
	 * @return the <code>Boolean</code> value
	 * @throws ErrorBean if the input datum is not parsable
	 */
	protected Boolean checkBoolean (String bool) throws ErrorBean {
		return Boolean.parseBoolean(bool);
	}
	
	/**
	 * Parses a <code>String</code> into a <code>Calendar</code>.
	 * @param calendar the <code>String</code>
	 * @param code the <code>ErrorCode</code> to throw and log when the <code>String</code> is unparsable
	 * @param caller the calling class
	 * @return the <code>Calendar</code> object
	 * @throws ErrorBean if the input datum is not parsable
	 */
	protected Calendar checkCalendar (String calendar, ErrorCode code, Control caller) throws ErrorBean {
		try {
			Calendar c = Calendar.getInstance();
			c.setTimeInMillis(CALENDAR_FORMAT.parse(calendar).getTime());
			return c;
		} catch (ParseException e) {
			throwError(
				code,
				calendar,
				caller);
			return null;
		}
	}
	
	/**
	 * Parses a <code>String</code> into a <code>Date</code>.
	 * @param date the <code>String</code>
	 * @param code the <code>ErrorCode</code> to throw and log when the <code>String</code> is unparsable
	 * @param caller the calling class
	 * @return the <code>Date</code> object
	 * @throws ErrorBean if the input datum is not parsable
	 */
	protected Date checkDate (String date, ErrorCode code, Control caller) throws ErrorBean {
		try {
			return DATE_FORMAT.parse(date);
		} catch (ParseException e) {
			throwError(
				code,
				date,
				caller);
			return null;
		}
	}
	
	/**
	 * Parses a <code>String</code> into a <code>Position</code>.
	 * @param position the <code>String</code>
	 * @param code the <code>ErrorCode</code> to throw and log when the <code>String</code> is unparsable
	 * @param caller the calling class
	 * @param logValues if <code>true</code> - log the input values
	 * @return the <code>Date</code> object
	 * @throws ErrorBean if the input datum is not parsable
	 */
	protected Position checkPosition (String position, ErrorCode code, Control caller, boolean logValues) throws ErrorBean {
		try {
			return Position.translate(position);
		} catch (IllegalArgumentException e) {
			if (logValues)
				throwError(
					code,
					caller);
			else
				throwError(
					code,
					position,
					caller);
			return null;
		}
	}
	
	/* END Methods */
	
}
